package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.QuestionMapper;
import com.tanhua.common.mapper.SettingsMapper;
import com.tanhua.common.pojo.Question;
import com.tanhua.common.pojo.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.management.Query;
import java.util.Date;

/**
 * @author 小霞
 * @version 1.0
 * @description: 陌生人问答
 * @date 2021/7/8 21:59
 */

@Service
public class QuestionService {


    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private SettingsMapper settingsMapper;

    public Question queryQuestion(Long userId) {
        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        Question question = this.questionMapper.selectOne(queryWrapper);
        return question;
    }

    /**
     * 查询问题
     *
     * @param userId
     * @param content
     */
    public void save(Long userId, String content) {
        //获取到用户问题
        Question question = this.queryQuestion(userId);
        //判断用户问题是非为空
        if (ObjectUtil.isEmpty(question)) {
            //要是为空，就直接新增
            question = new Question();
            question.setUserId(userId);
            question.setTxt(content);
            question.setCreated(new Date());
            question.setUpdated(question.getCreated());
            this.questionMapper.insert(question);
        } else {
            question.setTxt(content);
            this.questionMapper.updateById(question);
        }
    }

    /**
     * 查询通知
     *
     * @param userId
     * @return
     */
    public Settings querySettings(Long userId) {
        QueryWrapper<Settings> wrapper = new QueryWrapper<>();
        wrapper.eq("user_Id", userId);
        return this.settingsMapper.selectOne(wrapper);
    }
}
